package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Productattribute;

@Repository(value = "productAttributeDao")
@Transactional(readOnly = false)
public class ProductAttributeDaoImpl implements ProductAttributeDao {
	
	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	} 
	
	public List<Productattribute> getListProductAttribute() {
		TypedQuery<Productattribute> q = em.createNamedQuery("Productattribute.findAll",Productattribute.class);
    	return q.getResultList();
	}

	public Productattribute findProductAttributeByProductAttributeId(Integer productAttributeId) {
		Productattribute res=null;
		try {
			res=em.find(Productattribute.class,productAttributeId);
		} catch (Exception e) {
			// TODO: handle exception
		}
    	return res;
	}

	public void update(Productattribute productAttribute) {
    	/*
		Query  q = em.createQuery("UPDATE Productattribute p SET p.name=:name,p.image=:image,p.detail=:detail,p.order=:order WHERE p.productAttributeId = :productAttributeId");
		q.setParameter("productAttributeId",productAttribute.getProductAttributeId());
    	q.setParameter("name",productAttribute.getName());
		q.setParameter("detail",productAttribute.getDetail());
		q.setParameter("order",productAttribute.getOrder());
		q.setParameter("image",productAttribute.getImage());		
		q.executeUpdate();*/
		em.merge(productAttribute);
	}

	public void remove(Productattribute productAttribute) {
		em.remove(em.getReference(Productattribute.class,productAttribute.getProductAttributeId()));
	}
	
	public void add(Productattribute productAttribute) {
		em.persist(productAttribute);
	}
}
